package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 分类业务层
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 分类分页查询
     * @param dto
     * @return
     */
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO dto) {
        //1.开启分页查询
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        //2.调用mapper分页查询
        Page<Category> page = categoryMapper.pageQuery(dto);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增分类
     * @param dto
     */
    @Override
    public void save(CategoryDTO dto) {
        //补充确实的属性
        Category category = new Category();
        BeanUtils.copyProperties(dto, category);
        //使用公共字段自动填充逻辑
        categoryMapper.save(category);
    }

    /**
     * 修改分类
     * @param dto
     */
    @Override
    public void update(CategoryDTO dto) {
        Category category = new Category();
        BeanUtils.copyProperties(dto, category);
        //使用公共字段自动填充逻辑
        //直接调用mapper进行更新操作
        categoryMapper.update(category);

    }
    /**
     * 启用禁用分类
     * @param status
     * @param id
     */

    @Override
    public void startOrStop(Integer status, Long id) {
        //启用禁用分类实际上就是做更新操作
        Category category = Category.builder()
                .id(id)
                .status(status)
                .build();
        categoryMapper.update(category);
    }

    /**
     * 根据id删除分类
     * @param id
     */

    @Override
    public void deleteById(Long id) {
        //这里还需判断当前分类下是否有菜品或套餐
        //如果菜品跟种类绑定了，不能删
        int count = dishMapper.checkCountByCategoryId(id);
        if(count > 0)
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        //如果套餐跟种类绑在了，不能删
         count = setmealMapper.checkCountByCategoryId(id);
        if(count > 0)
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        categoryMapper.deletedById(id);
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @Override
    public List<Category> list(Integer type) {
       List<Category>list = categoryMapper.list(type);
       return list;
    }
}
